package com.wuwei.elearning.question.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuwei.elearning.question.dao.DataDAO;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.listener.UploadDataListener;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.utils.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 保存题目信息的表，不管是机型题，基础执照题，还是英语题，都保存在该表中 前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-01-20
 */
@RestController
@Slf4j
@RequestMapping("/elearning/question")
@CrossOrigin
public class QuestionController {

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private DataDAO<Question> toolDataDAO;

    @GetMapping("getById/{id}")
    public Result<Question> getById(@PathVariable("id") Long id){
        Question question = questionMapper.selectById(id);
        return Result.ok(question);
    }

    @PostMapping("update")
    public Result<String> update(@RequestBody Question question){
        questionMapper.updateById(question);
        return Result.ok("更新成功！");
    }

    @GetMapping("list/{name}/{currentPage}/{pageSize}")
    public Result<List<Question>> list(@PathVariable("name")String name, @PathVariable("currentPage") int currentPage,
                                       @PathVariable("pageSize") int pageSize){
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!"_".equals(name),Question::getQuestion,name);
        IPage<Question> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        IPage<Question> questionIPage = questionMapper.selectPage(page, wrapper);
        List<Question> questions = questionIPage.getRecords();
        return Result.ok(questions);
    }

    @GetMapping("getTotal/{name}")
    public Result<Long> getTotal(@PathVariable("name")String name){
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!"_".equals(name),Question::getQuestion,name);
        Long count = questionMapper.selectCount(wrapper);
        return Result.ok(count);
    }

    @PostMapping("add")
    public Result<String> add(@RequestBody Question question){
        questionMapper.insert(question);
        return Result.ok("添加成功！");
    }

    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable("id") Long id){
        questionMapper.deleteById(id);
        return Result.ok("删除成功！");
    }

    @PostMapping("upload")
    public Result<String> file(MultipartFile file){
        if (file.getSize() != 0) {
            try {
                EasyExcel.read(file.getInputStream(),Question.class,new UploadDataListener<Question>(toolDataDAO)).sheet().doRead();
                List<Question> list = toolDataDAO.getList();
                log.info("上传题目的总数为："+list.size());
                list.forEach(question -> questionMapper.insert(question));
                list.clear();
                return Result.ok("上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                return Result.fail(e.getMessage());
            }
        } else {
            return Result.fail("您没有上传任何文件");
        }
    }
}

